#include "stdafx.h"

ProgramMgr::ProgramMgr()
{
}

ProgramMgr::~ProgramMgr()
{
	for (std::pair<std::string, Program *> p : programs)
	{
		delete p.second;
	}
}

ProgramMgr * ProgramMgr::getInstance()
{
	static ProgramMgr manager;

	return &manager;
}

Program * ProgramMgr::get(const std::string & name)
{
	return programs[name];
}

void ProgramMgr::create(std::initializer_list<std::string> shader_names, const std::string & alias)
{
	std::vector<Shader *> shaders;
	Program * program = new Program;

	for (std::initializer_list<const std::string>::const_iterator it = shader_names.begin(); it != shader_names.end(); it++)
	{
		Shader * shader = new Shader(*it);
		shaders.push_back(shader);
		program->attach(shader);
	}
	program->link();
	programs[alias] = program;

	/*for (std::vector<Shader *>::iterator it = shaders.begin(); it != shaders.end(); it++)
	{
		delete *it;
	}*/
}

void ProgramMgr::use(const std::string & name)
{
	cur = get(name);
	cur->use();
}

Program * ProgramMgr::getCurrent()
{
	return cur;
}